var app = getApp();
Page({
    data: {
        takeaddress: ['三食堂二层', '二食堂一层'],
        index: 0,
        address: {
            name: "",
            phone: "",
            room: "",
            time: "请选择时间",
            note: "",
            like:''
        },
        before: 0,
        firstBuilding: '主楼',
        firstArea: 'D区',
        selectBuilding: true,
        selectArea: true,
        building: ['主楼', '教一(未开放)', '教二(未开放)', '教三(未开放)', '教四(未开放)', '教五(未开放)'],
        area: ['A区(未开放)', 'B区(未开放)', 'C区(未开放)', 'D区', 'E区(未开放)', 'F区(未开放)', 'G区(未开放)']
    },//点击选择类型
    bindPickerChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        
        this.setData({
            index: e.detail.value,
            'address.room':this.data.takeaddress[e.detail.value]
        })
        console.log('picker发送选择改变，携带值为', this.data)
    },
    clickBuilding: function () {
        var selectBuilding = this.data.selectBuilding;
        if (selectBuilding == true) {
            this.setData({
                selectBuilding: false,
            })
        } else {
            this.setData({
                selectBuilding: true,
            })
        }
    },
    clickArea: function () {
        var selectArea = this.data.selectArea;
        if (selectArea == true) {
            this.setData({
                selectArea: false,
            })
        } else {
            this.setData({
                selectArea: true,
            })
        }
    },
    //点击切换
    mySelect: function (e) {
        this.setData({
            selectBuilding: true,
            selectArea: true,
        })
    },
    onLoad: function (option) {
        this.data.before = option.before
        this.setData({
            before: option.before
        });
        var that = this
        wx.getStorage({
            key: 'address',
            success: function (res) {
                console.log(res.data)
                that.setData({
                    address: res.data
                });
            }
        })
    },
    onShow: function () {
        this.setData({
            userInfo: app.globalData.userInfo
        });
    },
    onTest: function () {
        console.log("test");
    },
    bindTimeChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            'address.time': e.detail.value
        })
    },
    saveAddress: function () {
        wx.setStorage({
            key: "address",
            data: this.data.address,
            success: function (res) {
                console.log(res.data)
            }
        })
        if (this.data.before == 0) {
            wx.showModal({
                showCancel: false,
                title: '收货地址',
                content: '地址保存成功',
                success: function (res) {
                    if (res.confirm) {
                        wx.navigateBack();
                    }
                }
            })
        } else {
            var pages = getCurrentPages();
            var prevPage = pages[pages.length - 2];  //上一个页面
            console.log(prevPage.data.cart);
            wx.request({
                url: app.globalData.serverAddress + '/order/apis/addOrder',
                method: 'POST',
                data: {
                    // order: this.data.address
                    order: {
                        food_id: prevPage.data.cart,
                        order_name: this.data.address.name,
                        order_phone: this.data.address.phone,
                        order_room: this.data.address.room,
                        order_time: this.data.address.time,
                        order_note: this.data.address.note,
                    }
                },
                header: {
                    'content-type': 'application/json' // 默认值
                },
                success: function (res) {
                    console.log(res.data)
                    wx.showModal({
                        showCancel: false,
                        title: '恭喜',
                        content: '订单发送成功！下订单过程顺利完成，请刷校园卡支付。',
                        success: function (res) {
                            if (res.confirm) {
                                wx.navigateBack(2);
                            }
                        }
                    });
                },
                fail: function (res) {
                    console.log(res)
                    wx.showModal({
                        showCancel: false,
                        title: '提示',
                        content: '下单失败'
                    });
                }
            })
        }
    },
    inputPerson: function (e) {
        this.data.address.name = e.detail.value
    },
    inputPhone: function (e) {
        this.data.address.phone = e.detail.value
    },
    inputRoom: function (e) {
        this.data.address.room = e.detail.value
    },
    inputNote: function (e) {
        this.data.address.note = e.detail.value
    },
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh()
    }
});